﻿using AutoMapper;
using B.S.Sterilizationcenter.Domain.zmc;
using B.S.Sterilizationcenter.ErrorCode;
using B.S.Sterilizationcenter.Writer.API.Appliaction.Commends.Zmc.Warehou;
using MediatR;
using SqlSugar;

namespace B.S.Sterilizationcenter.Writer.API.Appliaction.CommandHandlers.Zmc.Warehou
{
    public class WarehouStatusCommendHandlers : IRequestHandler<WarehouStatusCommends, ApiResult<int>>
    {

        private readonly ISqlSugarClient db;
        private readonly IMapper mapper;

        public WarehouStatusCommendHandlers(ISqlSugarClient db, IMapper mapper)
        {
            this.db = db;
            this.mapper = mapper;
        }


        /// <summary>
        /// 修改入库状态
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ApiResult<int>> Handle(WarehouStatusCommends request, CancellationToken cancellationToken)
        {
            try
            {
                ApiResult<int> result = new ApiResult<int>();
                var res = db.Queryable<Warehous>().FirstAsync(p => p.WarehousNo == request.WarehousNo).Result;
                res.Status = 2;
                res.AuditDate = DateTime.Now;
                var newres= db.Updateable(res).ExecuteCommand();
                if (newres != null)
                {
                    result.Code = ApiEnums.修改成功;
                    result.Msg= "修改成功";
                    result.Data = newres;

                }
                else
                {
                    result.Code = ApiEnums.修改失败;
                    result.Msg = "修改失败";
                }

                return result;
            }
            catch (Exception)
            {

                throw;
            }

        }
    }
}
